import axios from 'axios';
import { useUserStore } from '@/stores/user.js';
import router from '@/router';
import { ElMessage } from 'element-plus';
// http://159.75.169.224:7300/pz/user/authentication
// const baseUrl = 'https://v3pz.itndedu.com/v3pz';
const baseUrl = 'http://159.75.169.224:7300';
const instance = axios.create({
  baseURL: baseUrl,
  timeout: 5000
});
// 请求拦截
instance.interceptors.request.use(
  (config) => {
    const useStore = useUserStore();
    const token = useStore.token;
    const whiteUrl = ['/get/code', '/login', '/user/authentication'];
    if (!whiteUrl.includes(config.url) && token) {
      config.headers['x-token'] = token;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);
// 响应拦截
instance.interceptors.response.use(
  (res) => {
    if (res.data.code === -1) {
      ElMessage.warning(res.data.message.msg || '服务异常');
    }
    return res.data;
  },
  (Error) => {
    // 401错误
    // if (Error.response?.status === 401) {
    //   router.push('/login');
    //   ElMessage.error('登录失效，请重新登录');
    // }
    // // 错误默认情况
    // ElMessage.error(Error.response?.data?.message || '服务异常');
    return Promise.reject(Error);
  }
);
export default instance;
export { baseUrl };
